package com.sjq.common.web;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;

import com.sjq.common.exception.DataAccessException;
import com.sjq.common.exception.ServiceException;
import com.sjq.common.util.DateFormatUtil;

@ControllerAdvice
public class AllExceptionControl {
	
	private static final Logger logger = Logger.getLogger(AllExceptionControl.class);
	
	@ExceptionHandler(DataAccessException.class)
	public ModelAndView opearteDaoException(DataAccessException rx,HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		logger.error(rx.getMessage(), rx);
		logger.info("*****----异常信息已记录("+DateFormatUtil.dateFormat()+")----*****");
		request.setAttribute("errorTips", rx.getMessage());
		request.setAttribute("rx", rx);
		map.put("msg", "dao层数据异常:"+rx.getMessage());
		map.put("error", rx);
		StackTraceElement[] stackTrace = rx.getStackTrace();
		StringBuilder stackMsg = new StringBuilder();
		for (StackTraceElement ele : stackTrace) {
			String msg = String.format(" %s %n", ele.toString());
			stackMsg.append(msg);
		}
		map.put("stack", stackMsg.toString());
		return new ModelAndView("/view/error/globalErrorPage",map);
	}
	
	@ExceptionHandler(ServiceException.class)
	public ModelAndView opearteServiceException(ServiceException rx,HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		logger.error(rx.getMessage(), rx);
		logger.info("*****----异常信息已记录("+DateFormatUtil.dateFormat()+")----*****");
		request.setAttribute("errorTips", rx.getMessage());
		request.setAttribute("rx", rx);
		map.put("msg", "service层业务异常:"+rx.getMessage());
		map.put("error", rx);
		StackTraceElement[] stackTrace = rx.getStackTrace();
		StringBuilder stackMsg = new StringBuilder();
		for (StackTraceElement ele : stackTrace) {
			String msg = String.format(" %s %n", ele.toString());
			stackMsg.append(msg);
		}
		map.put("stack", stackMsg.toString());
		return new ModelAndView("/view/error/globalErrorPage",map);
	}
	
	@ExceptionHandler(Exception.class)
	public ModelAndView opearteException(Exception rx,HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		logger.error(rx.getMessage(), rx);
		logger.info("*****----异常信息已记录("+DateFormatUtil.dateFormat()+")----*****");
		request.setAttribute("errorTips", rx.getMessage());
		request.setAttribute("rx", rx);
		map.put("msg", rx.getMessage());
		map.put("error", rx);
		StackTraceElement[] stackTrace = rx.getStackTrace();
		StringBuilder stackMsg = new StringBuilder();
		for (StackTraceElement ele : stackTrace) {
			String msg = String.format(" %s %n", ele.toString());
			stackMsg.append(msg);
		}
		map.put("stack", stackMsg.toString());
		return new ModelAndView("/view/error/globalErrorPage",map);
	}
	
	@ExceptionHandler(Error.class)
	public ModelAndView opearteError(Error rx,HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		logger.error(rx.getMessage(), rx);
		logger.info("*****----异常信息已记录("+DateFormatUtil.dateFormat()+")----*****");
		request.setAttribute("errorTips", rx.getMessage());
		request.setAttribute("rx", rx);
		map.put("msg", rx.getMessage());
		map.put("error", rx);
		StackTraceElement[] stackTrace = rx.getStackTrace();
		StringBuilder stackMsg = new StringBuilder();
		for (StackTraceElement ele : stackTrace) {
			String msg = String.format(" %s %n", ele.toString());
			stackMsg.append(msg);
		}
		map.put("stack", stackMsg.toString());
		return new ModelAndView("/view/error/globalErrorPage",map);
	}
	
	@ExceptionHandler(Throwable.class)
	public ModelAndView opearteThrowable(Throwable rx,HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		logger.error(rx.getMessage(), rx);
		logger.info("*****----异常信息已记录("+DateFormatUtil.dateFormat()+")----*****");
		request.setAttribute("errorTips", rx.getMessage());
		request.setAttribute("rx", rx);
		map.put("msg", rx.getMessage());
		map.put("error", rx);
		StackTraceElement[] stackTrace = rx.getStackTrace();
		StringBuilder stackMsg = new StringBuilder();
		for (StackTraceElement ele : stackTrace) {
			String msg = String.format(" %s %n", ele.toString());
			stackMsg.append(msg);
		}
		map.put("stack", stackMsg.toString());
		return new ModelAndView("/view/error/globalErrorPage",map);
	}

}
